package com.dai.zhenfujin;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dai.zhenfujin.Constants.Constants;
import com.dai.zhenfujin.base.BaseActivity;
import com.dai.zhenfujin.bean.ResBdBean;
import com.dai.zhenfujin.bean.ResultBean2;
import com.dai.zhenfujin.http.CallBack;
import com.dai.zhenfujin.http.HttpClient;
import com.dai.zhenfujin.utils.UIUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 找回密码
 */
public class RetrievePasswordActivity extends BaseActivity implements View.OnClickListener {

    private EditText mRegister_number, register_img;
    private TextView mRegister_verification, title_textview;
    private EditText mRegister_password;
    private EditText mRegister_sms;
    private Button mRegister_button;
    private FrameLayout fl_img;
    private ImageView register_show_img;
    private String verifyId;
    private VerificationCodeCountTimer verificationCodeCountTimer;
    private String ver_env;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password);

        mRegister_number = (EditText) findViewById(R.id.register_number);
        mRegister_verification = (TextView) findViewById(R.id.register_verification);
        mRegister_password = (EditText) findViewById(R.id.register_password);
        mRegister_sms = (EditText) findViewById(R.id.register_sms);
        mRegister_button = (Button) findViewById(R.id.register_button);
        fl_img = findViewById(R.id.fl_img);
        title_textview = findViewById(R.id.title_textview);
        register_show_img = findViewById(R.id.register_show_img);
        register_img = findViewById(R.id.register_img);
        title_textview.setText("找回密码");
        verificationCodeCountTimer = new VerificationCodeCountTimer(61100, 1000);

        mRegister_number.setOnClickListener(this);
        mRegister_verification.setOnClickListener(this);
        //获取验证码
        register_show_img.setOnClickListener(this);
        mRegister_button.setOnClickListener(this);
        fl_img.setOnClickListener(this);
        //得到图形验证码
        getVerify();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //验证码
            case R.id.register_verification:
                getCode();
                break;
            //找回密码
            case R.id.register_button:
                forgetPwd();
                break;
            case R.id.fl_img:
                finish();
                break;
            case R.id.register_show_img: //图形
                getVerify();
                break;
        }
    }

    private void getVerify() {
        Map<String, Object> params = new HashMap<>();
        Map<String, String> params2 = new HashMap<>();
        params.put("req_bd", params2);
        HttpClient.post(this, Constants.CODE_URL, params, new CallBack<ResBdBean>() {
            @Override
            public void onSuccess(ResBdBean resultBean) {
                verifyId = resultBean.verifyId;
                ver_env = resultBean.ver_env;
                Glide.with(RetrievePasswordActivity.this).load(resultBean.url).into(register_show_img);

            }
        });
    }


    /**
     * 找回密码
     */
    private void forgetPwd() {
        if (TextUtils.isEmpty(mRegister_number.getText().toString().trim())) {
            UIUtils.showToastSafe("请输入手机号码");
            return;
        }

        if (TextUtils.isEmpty(register_img.getText().toString().trim())) {
            UIUtils.showToastSafe("请输入图形验证码");
            return;
        }

        if (TextUtils.isEmpty(mRegister_password.getText().toString().trim())) {
            UIUtils.showToastSafe("请输入验证码");
            return;
        }


        if (TextUtils.isEmpty(mRegister_sms.getText().toString().trim())) {
            UIUtils.showToastSafe("请输入6-12位密码");
            return;
        }

        if ( mRegister_sms.getText().toString().length() < 6) {
            UIUtils.showToastSafe("请输入6-12位密码");
            return;
        }

        Map<String, Object> params = new HashMap<>();
        Map<String, String> params2 = new HashMap<>();

        params2.put("mobile", mRegister_number.getText().toString().trim());
        params2.put("code", mRegister_password.getText().toString().trim()); //验证码
        params2.put("pwd", UIUtils.Md5(mRegister_sms.getText().toString().trim()));

        params.put("req_bd", params2);

        HttpClient.post(this, Constants.FORGETPWD, params, new CallBack<ResultBean2>() {
            @Override
            public void onSuccess(ResultBean2 resultBean) {
                UIUtils.showToastSafe("找回密码成功");
                finish();
            }
        });
    }


    private void getCode() {
        if (!UIUtils.isMobileNO(mRegister_number.getText().toString().trim())) {
            UIUtils.showToastSafe("请输入手机号");
            return;
        }

        if (TextUtils.isEmpty(register_img.getText().toString().trim())) {
            UIUtils.showToastSafe("请输入图形验证码");
            return;
        }

        //verificationCodeCountTimer.start();
        Map<String, Object> params = new HashMap<>();
        Map<String, String> params2 = new HashMap<>();

        params2.put("mobile", mRegister_number.getText().toString().trim());
        params2.put("type", "3");
        params2.put("verifyId", verifyId);
        params2.put("ver_env", ver_env);
        params2.put("verifyCode", register_img.getText().toString().trim());
        params.put("req_bd", params2);

        HttpClient.post(this, Constants.GETCODE, params, new CallBack<ResBdBean>() {
            @Override
            public void onSuccess(ResBdBean resultBean) {
                if (resultBean.getSms_code().equals("true")){
                    UIUtils.showToastSafe("验证码已发送");
                    verificationCodeCountTimer.start();
                }else {
                    UIUtils.showToastSafe("获取过于频繁，请稍后一分钟后在试");
                }
            }
        });
    }

    class VerificationCodeCountTimer extends CountDownTimer {

        private VerificationCodeCountTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            int time = (int) (Math.round((double) millisUntilFinished / 1000) - 1);
            mRegister_verification.setTextColor(getResources().getColor(R.color.btn_blue_normal));
            mRegister_verification.setText(time + "s");
        }

        @Override
        public void onFinish() {
            mRegister_verification.setText("获取验证码");
        }
    }
}
